﻿using System;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Development.Network.Msg;
using Development.Tools;
using System.Collections.Specialized;
using System.Web;


namespace Development.Network.Http
{
    public class HttpHelper
    {
        public static void AsyncRequstPacket(byte[] data, string url, Action<MsgPacket> action)
        {
            new Task(() =>
            {
                try
                {
                    WebClient m_web = new WebClient();
                    byte[] recvData = m_web.UploadData(url, data);
                    if (recvData.Length < 4)
                    {
                        Log.WriteLog(ELogLevel.LOG_ERROR, "请求包失败");
                        return;
                    }
                    MsgPacket packet = MsgPacket.SplitPacket(null,recvData, 0, recvData.Length);
                    m_web.Dispose();
                    if (action != null)
                    {
                        action(packet);
                    }

                }
                catch (WebException e)
                {
                    if (action != null)
                    {
                        action(null);
                    }
                    Log.WriteLog(ELogLevel.LOG_ERROR, e.Message);
                }
            }).Start();
        }
        public static void AsyncRequstServer(byte[] data,string url,Action<byte[]> action)
        {
            new Task(() =>
            {
                try
                {
                    WebClient m_web = new WebClient();
                    byte[] recvData = m_web.UploadData(url, data);
                    m_web.Dispose();
                    if (action != null)
                    {
                        action(recvData);
                    }
                   
                }
                catch (WebException e)
                {
                    if (action != null)
                    {
                        action(null);
                    }
                    Log.WriteLog(ELogLevel.LOG_ERROR, e.Message);
                }
            }).Start();
        }

        public static void RequstServer(byte[] data, string url, Action<MsgPacket> action)
        {
            try
            {
                WebClient m_web = new WebClient();
                byte[] recvData = m_web.UploadData(url, data);
                m_web.Dispose();
                if (recvData.Length < 4)
                {
                    Log.WriteLog(ELogLevel.LOG_ERROR, "请求包失败");
                    return;
                }
                MsgPacket packet = MsgPacket.SplitPacket(null, recvData, 0, recvData.Length);
                if (action != null)
                {
                    action(packet);
                }

            }
            catch (WebException e)
            {
                if (action != null)
                {
                    action(null);
                }
                Log.WriteLog(ELogLevel.LOG_ERROR, e.Message);
            }
        }

        public static void RequstServer(byte[] data, string url, Action<byte[]> action)
        {
            try
            {
                WebClient m_web = new WebClient();
                byte[] recvData = m_web.UploadData(url, data);
                m_web.Dispose();
                if (action != null)
                {
                    action(recvData);
                }

            }
            catch (WebException e)
            {
                if (action != null)
                {
                    action(null);
                }
                Log.WriteLog(ELogLevel.LOG_ERROR, e.Message);
            }
        }

        public static NameValueCollection GetNameValues(System.Net.HttpListenerRequest requst)
        {
            string querystring = "";
            if (requst.HttpMethod == "POST")
            {
                byte[] buffer = new byte[requst.ContentLength64];
                requst.InputStream.Read(buffer, 0, (int)requst.ContentLength64);
                querystring = Encoding.UTF8.GetString(buffer);
            }
            else if (requst.HttpMethod == "GET")
            {
                int index = requst.RawUrl.IndexOf('?');
                if (index != -1)
                {
                    querystring = requst.RawUrl.Remove(0, index + 1);
                }
            }
            NameValueCollection nameValues = HttpUtility.ParseQueryString(querystring);
            return nameValues;
        }

        public static string GetJsonString(System.Net.HttpListenerRequest requst)
        {
            byte[] buffer = new byte[requst.ContentLength64];
            requst.InputStream.Read(buffer, 0, (int)requst.ContentLength64);
            return Encoding.UTF8.GetString(buffer);
        }
    }
}
